package com.haogu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.haogu.mapper.ContactsMapper;
import com.haogu.mapper.CustomerMapper;

import com.haogu.mapper.UserMapper;
import com.haogu.service.CustomersService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CustomerService implements CustomersService {
    @Resource
    private UserMapper systemUserMapper;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private ContactsMapper contactsMapper;

    @Resource
    private JavaMailSender javaMailSender;

    /**
     * 查询所有系统用户
     *
     * @return
     */
    public List<User> selectListUser() {
        List<User> userList = systemUserMapper.selectList(null);
        System.out.println();
        return userList;
    }

    /**
     * 插入客户
     *
     * @param customer
     */
    public void insertcustomer(Customer customer) {
        customerMapper.insert(customer);
    }

    /**
     * 插入联系人
     *
     * @param contacts
     */
    @Override
    public void insertContacts(Contacts contacts) {

        contactsMapper.insert(contacts);
    }

    /**
     * 查询所有客户
     *
     * @return
     */
    @Override
    public List<CustomerList> selectCustomerList(Integer page, Integer size) {
        return customerMapper.selectCustomerList((page - 1) * size, size);
    }

    /**
     * 查询用户总数
     *
     * @return
     */
    @Override
    public int rows() {
        QueryWrapper<Customer> ow = new QueryWrapper<>();
        ow.isNotNull("owner_user_id");
        return customerMapper.selectCount(ow);
//        return customerMapper.selectCount(null);
    }
    /**
     * 查询客户池总数
     *
     * @return
     */
    @Override
    public int rows1() {
        QueryWrapper<Customer> ow = new QueryWrapper<>();
        ow.isNull("owner_user_id");
        return customerMapper.selectCount(ow);
//        return customerMapper.selectCount(null);
    }

    /**
     * 查询客户某一条信息
     *
     * @param id
     * @return
     */
    @Override
    public Customer selectCustomer(Integer id) {
//        return customerMapper.selectCustomer(id);
        return customerMapper.selectById(id);
    }

    /**
     * 修改,查询负责人
     *
     * @param
     * @return
     */
    @Override
    public List<User> selectByUser() {
        return customerMapper.selectByUser();
    }

    /**
     * 修改
     *
     * @param customer
     */
    @Override
    public void change(Customer customer) {
        customerMapper.updateById(customer);
    }

    /**
     * 客户根据条件模糊查询
     *
     * @param search
     * @return
     */
    @Override
    public List<CustomerList> selectByCustomer(String search, String filterSearch) {
        Integer va = Integer.valueOf(filterSearch);
        List<CustomerList> listList = null;
        if (va == 0 || va == 1 || va == 2 || va == 3) {
            listList = customerMapper.selectCustomerLike(search, filterSearch);
        } else {
            return null;
        }
        return listList;
    }
    /**
     * 客户池根据条件模糊查询
     *
     * @param search
     * @return
     */
    @Override
    public List<CustomerList> selectByCustomers(String search, String filterSearch) {
        Integer va = Integer.valueOf(filterSearch);
        List<CustomerList> listList = null;
        if (va == 0 || va == 1 || va == 2 || va == 3) {
            listList = customerMapper.selectCustomersLike(search, filterSearch);
        } else {
            return customerMapper.selectCustomers();
        }
        return listList;
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public int delCustomers(Integer id) {
        int i = customerMapper.deleteById(id);
        return i;
    }

    /**
     * 填充删除时间及删除者的id
     *
     * @param id
     */
    @Override
    public void deleteTime(Integer deleteId, Integer id) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        customerMapper.deleteTime(deleteId, format, id);
    }

    /**
     * 邮件发送
     *
     * @param from    发送者
     * @param to      接收者
     * @param subject 邮件标题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String from, String to, String subject, String content) {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);//发送者
        message.setTo(to);//接收者
        message.setSubject(subject);//邮件标题
        message.setText(content);//邮件内容

        try {
            javaMailSender.send(message);
            logger.info("简单邮件已经发送。");
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常！", e);
        }
    }

    /**
     * 今日新建查询
     *
     * @return
     */
    @Override
    public List<CustomerList> todayNewCreateSelect(String type) {
        List<CustomerList> customerLists = null;
        if ("day".equals(type) || "week".equals(type) || "month".equals(type)) {
            customerLists = customerMapper.todayNewCreateSelect(type);
        }
        return customerLists;
    }

    /**
     * 我的客户查询
     * @param cid
     * @return
     */
    @Override
    public List<CustomerList> myCustomer(Integer cid) {
        return customerMapper.myCustomer(cid);
    }

    /**
     * 客户池查询
     * @return
     */
    @Override
    public List<CustomerList> selectCustomers() {
        return customerMapper.selectCustomers();
    }

    /**
     * 将客户放入到客户池
     * @param id
     * @return
     */
    @Override
    public int toReleaseCustomers(Integer id) {
        return customerMapper.toReleaseCustomers(id);
    }

    /**
     * 批量关注
     * @param id
     * @return
     */
    @Override
    public int toBatchConcerned(Integer id) {
        return customerMapper.toBatchConcerned(id);
    }

    /**
     * 关注查询
     * @return
     */
    @Override
    public List<CustomerList> selectBatchConcerned() {
        return customerMapper.selectBatchConcerned();
    }

    /**
     * 改变关注图标
     * @param id
     * @return
     */
    @Override
    public Customer cuss(Integer id) {
        return customerMapper.selectById(id);
    }

    /**
     * 批量取消关注
     * @param id
     * @return
     */
    @Override
    public List<CustomerList> toCancelConcerned(Integer id) {
        return customerMapper.toCancelConcerned(id);
    }

    /**
     * 定时将没有维护的客户放入到客户池
     * @return
     */
    @Override
    public List<Customer> customerPool() {
        QueryWrapper<Customer> ow = new QueryWrapper<>();
        ow.isNotNull("owner_user_id");
        List<Customer> customers = customerMapper.selectList(ow);
        return customers;
    }

    /**
     * 查询发送短信相关信息
     * @param id
     * @return
     */
    @Override
    public DuanXin selectChat(Integer id) {
        return customerMapper.selectChat(id);
    }
}
